﻿package com.xeem.henkel.common
{
	import com.xeem.core.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	
	public class BarComponent extends  UIBaseComponent
	{
		private var _bar:Sprite;
		private var _btn:Sprite;
		
		private var _title:TextField;
		
		private var _txtValue:TextField;
		
		private var _w:Number;
		private var _h:Number;
		private var _unit:Number;
		
		private var _languageUnit:LanguageModel;
		private var styler:NumberFormat=new NumberFormat();

		private var _minUnit:Number=0;
		
		public function BarComponent(contain:DisplayObjectContainer,w:Number,unit:Number=5,minUnit:Number=0,h:Number=5)
		{
			
			if(contain!=null)   
				contain.addChild(this);   			
			this._w=w;
			this._h=h;
			this._unit=unit;			
			this._minUnit=minUnit;
			this.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			stage.stageFocusRect=false;
		}
		
		private function keyDownHandler(event:KeyboardEvent):void {
			//this.value=(int)(_btn.x/(_w/(data.rangeData/_minUnit)))*_minUnit+data.minData;
			if (event.keyCode==39){
				if (_minUnit==0)
					_btn.x=(this.value<data.minData+data.rangeData?this.value-data.minData+1/(data.precision==0?1:Math.pow(10,data.precision)):data.rangeData)*_w/data.rangeData;
				else{
					
					if (this.value+_minUnit<data.rangeData)
						_btn.x=((this.value/_minUnit+1)/(data.rangeData/_minUnit))*_w;
					else
						_btn.x=_w;
				}
			}
			if (event.keyCode==37){
				if (_minUnit==0)
					_btn.x=(this.value>data.minData?this.value-data.minData-1/(data.precision==0?1:Math.pow(10,data.precision)):0)*_w/data.rangeData;
				else
					_btn.x=((this.value>data.minData?this.value-data.minData-_minUnit:0)/_minUnit)*(_w/(data.rangeData/_minUnit));
			}
			reDraw();			
		}
		
		
		
		public function draw():void{
			drawData();
			//drawLanguage();
		}
		
		//画进度栏
		private function drawBar(w:Number,h:Number,unit:Number){
			_bar=new Sprite();
			var  xx:Number=5;
			clearBar(w,h);
			_bar.graphics.lineStyle(1,0xCFCFCF);
			var xunit=w/unit/xx;
			for (var i:Number=0;i<=unit*xx;i++){
				_bar.graphics.moveTo(i*xunit,2+h+1);
				if (i%xx==0)
					_bar.graphics.lineTo(i*xunit,2+h+4);
				else
					_bar.graphics.lineTo(i*xunit,2+h+3);
			}	
		}
		
		
		public override function drawData():void
		{
			_languageUnit=new LanguageModel();
			_languageUnit.fontSize="10";
			_languageUnit.text=data.minData+" "+data.unit;			
			
			_txtValue=_languageUnit.getTextField();			
			_txtValue.x=0;
			_txtValue.y=23+_h;	
			
			drawBar(this._w,this._h,this._unit);
			
			_bar.x=2;
			_bar.y=20;
			
			_bar.buttonMode=true;
			_bar.addEventListener(MouseEvent.CLICK,onClick);
			_btn=new Sprite();

			_btn.buttonMode=true;
			_btn.graphics.beginFill(0x0088d0);
			_btn.graphics.moveTo(0,0);
			_btn.graphics.lineTo(6, 0);
			_btn.graphics.lineTo(3,6);
			_btn.graphics.lineTo(0,0);
			_btn.graphics.endFill();
			
			_btn.graphics.beginFill(0xff0000,0);			
			_btn.graphics.drawRect(-3,-3,12,12);			
			_btn.graphics.endFill();
			
			_btn.x=0;
			_btn.y=15;
			addChild(_txtValue);			
			addChild(_btn);
			addChild(_bar);
			if (data.dataDef!=0){
				if (_minUnit==0)
					_btn.x=(data.dataDef-data.minData)*_w/data.rangeData;
				else
					_btn.x=(data.dataDef-data.minData/_minUnit)*(_w/(data.rangeData/_minUnit));
			}			
			reDraw();
			_btn.addEventListener(MouseEvent.MOUSE_DOWN, onStartDragHandler);
			_btn.stage.addEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);   
		}   
		
		private function onStartDragHandler(event:MouseEvent):void  
		{   
			stage.focus=Sprite(event.target);	
			_btn.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);   
			_btn.startDrag(false,new Rectangle(0,_btn.y,_w,0));//控制拖动局域  
		}   
		
		private function onMoveHandler(e:MouseEvent):void  
		{              
					
			reDraw();
		}   
		
		private function onStopDragHandler(event:MouseEvent):void  
		{   
			_btn.stopDrag();   
			_btn.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);   
		}   
		
		
		
		public override function drawLanguage():void
		{
			_title=language.getTextField();	
			_title.height=16;
			_title.x=0;
			_title.y=0;
			addChild(_title);
		}
		
		private function onClick(e:MouseEvent):void{
			stage.focus=Sprite(e.target);
			_btn.x=e.localX;
			reDraw();
		}
		
		private function clearBar(w:Number,h:Number):void{
			var myMatrix:Matrix=new Matrix();			
			myMatrix.createGradientBox(w,h+4,Math.PI/2,0,0);
			var myColors:Array = [0x717171, 0xDCDCDC,0xFfFfFf,0xDCDCDC,0x717171];
			var myAlphaS:Array = [1,1,1,1,1];
			var myRalphaS:Array = [0,51,128,204,255];
			_bar.graphics.beginGradientFill(GradientType.LINEAR, myColors, myAlphaS, myRalphaS, myMatrix);
			_bar.graphics.drawRoundRectComplex(0,2,w,h,3,3,2,2);
			_bar.graphics.lineStyle(1,0xCFCFCF);
		}
		
		public override function reDraw():void
		{
			clearBar(_w,_h);
			_bar.graphics.beginFill(0x0088d0);
			_bar.graphics.drawRoundRectComplex(0,2,_btn.x,_h,3,3,3,3);
			_bar.graphics.endFill();
			this.value=(_btn.x)*data.rangeData/(_w)+data.minData;
			if (_minUnit!=0){
				if (this.value%_minUnit>0)
					this.value=((int)(this.value/_minUnit)+1)*_minUnit;
			}
			
			if (data.precision==0){
				this.value=Math.round(this.value);				
			}else{
				this.value=Math.round(this.value*Math.pow(10,data.precision))/Math.pow(10,data.precision);
			}
			//_txtValue.text=styler.format(this.value,new Locale("en","US"))+" "+data.unit;	
			_txtValue.text=div(this.value,data.precision)+" "+data.unit;	
			_txtValue.setTextFormat(_languageUnit.getTextFormat());
			var evt:Event=new Event(Event.CHANGE);   
			this.dispatchEvent(evt); 
		}
		
		private function round(x:Number,p:int):Number{
			if (p==0){
				return Math.round(x);
			}else{
				return Math.round(x*Math.pow(10,p))/Math.pow(10,p);
			}
		}
		
		private function div(x:Number,p:int):String{
			if (p==0) return styler.format(x,new Locale("en","US"));
			var t:String="";
			var ii:int=0;
			for (;ii<=p;ii++){
				if (x*Math.pow(10,ii)==int(x*Math.pow(10,ii))) break;
			} 
			trace("ii=>"+ii.toString());
			if (ii<p){
				if (ii==0)t+=".";
				while(ii<p){
					t+="0";
					ii++;
				}
				return styler.format(x,new Locale("en","US"))+t;
			}else{
				return styler.format(x,new Locale("en","US"));
			}			
		}
	}
}